<template>
  <page-header-wrapper>
    <global-search :list="searchList" @search="handleSearch" @reset="handleReset"> </global-search>
    <a-card :bordered="false">
      <s-table
        ref="table"
        size="default"
        rowKey="ID"
        :columns="columns"
        :data="loadData"
        :pagination="{ showTotal: (total) => `共 ${total} 条` }"
        :scroll="{ x: true }"
        :row-selection="{ selectedRowKeys: selectedRowKeys, onChange: onTableSelectChange }"
      >
        <div slot="tabAction">
          <y-button type="primary" @click="handleMultiple">确认所选</y-button>
          <y-button type="primary" @click="handleMultiple('all')">确认全部</y-button>
        </div>
        <span slot="index" slot-scope="text, record, index">{{ index + 1 }} </span>
        <span slot="time" slot-scope="text">{{ text | moment }}</span>
        <ellipsis :length="46" slot="ellipsis" slot-scope="text">{{ text }}</ellipsis>
        <div slot="action" slot-scope="text, record">
          <a-popconfirm placement="left" title="确定要空置该房屋吗？" @confirm="handleAction(record, 'out')">
            <y-button type="link">审核</y-button>
          </a-popconfirm>
        </div>
      </s-table>
    </a-card>
    <formModal
      title="房源空置"
      label="房源空置原因"
      remarkRequired
      :visible.sync="visibleForm"
      :data.sync="editData"
      mode="ConfirmHouses"
      :filedName="{
        HouseID: 'ID',
        ProposerID: 'ProposerID',
      }"
      @success="tableRefresh"
    />
    <housesModal
      title="房源空置"
      label="房源空置原因"
      remarkRequired
      :visible.sync="visibleHouse"
      :data.sync="editDataHouse"
      mode="ConfirmHouses"
      :filedName="{
        HouseID: 'ID',
        ProposerID: 'ProposerID',
      }"
      @success="tableRefresh"
    />
  </page-header-wrapper>
</template>

<script>
import { STable, Ellipsis } from '@/components'
import { getConfirmHousesList, postExitHouses } from '@/api/followUpManagement'
import formModal from '../components/modal.vue'
import housesModal from './modal'
export default {
  components: {
    STable,
    Ellipsis,
    formModal,
    housesModal,
  },
  data() {
    return {
      searchList: [
        {
          label: '小区名称',
          fields: '小区名称',
          type: 'input',
          props: {
            placeholder: '请输入小区名称',
          },
        },
        {
          label: '腾退时间',
          fields: 'time',
          type: 'range',
          props: {
            valueFormat: 'YYYY-MM-DD',
            format: 'YYYY-MM-DD',
          },
        },
      ],
      queryParam: {
        Mode: 'ConfirmHouses',
        UserID: this.$store.getters.userid,
        strContent: {},
        DeptNO: this.$store.getters.userInfo.DeptNO,
      },
      columns: [
        {
          title: '序号',
          align: 'center',
          width: '80px',
          scopedSlots: { customRender: 'index' },
        },
        {
          title: '安置点',
          align: 'center',
          dataIndex: '安置点',
        },
        {
          title: '项目名称',
          align: 'center',
          dataIndex: '项目',
        },
        {
          title: '区域',
          align: 'center',
          dataIndex: '分管区级',
        },
        {
          title: '小区名称',
          align: 'center',
          dataIndex: '小区名称',
        },
        {
          title: '面积',
          align: 'center',
          dataIndex: '面积',
          customRender: (text) => `${text}㎡`,
        },
        {
          title: '房屋结构',
          align: 'center',
          dataIndex: '结构',
        },
        {
          title: '房屋地址',
          align: 'center',
          customRender: (text, record) => `${record.栋}栋${record.单元}单元${record.楼层}-${record.附号}`,
        },
        {
          title: '房屋状态',
          align: 'center',
          dataIndex: '状态',
        },
        {
          title: '腾退空置时间',
          align: 'center',
          dataIndex: '腾退空置时间',
        },
        {
          title: '操作',
          align: 'center',
          width: 80,
          fixed: 'right',
          scopedSlots: { customRender: 'action' },
        },
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: (parameter) => {
        this.selectedRowKeys = []
        const requestParameters = Object.assign({}, parameter, this.queryParam)
        return getConfirmHousesList(requestParameters).then((res) => res)
      },
      selectedRowKeys: [],
      editData: {},
      editDataHouse: {},
      visibleForm: false,
      visibleHouse: false,
      houseTypeOptions: [],
    }
  },
  methods: {
    handleSearch(values) {
      if (values.time) {
        this.queryParam.StartTime = values.time[0]
        this.queryParam.EndTime = values.time[1]
      }
      delete values.time
      this.queryParam.strContent = values
      this.tableRefresh(true)
    },
    onTableSelectChange(selectedRowKeys) {
      this.selectedRowKeys = selectedRowKeys
    },
    handleReset() {
      this.queryParam.strContent = {}
      this.queryParam.StartTime = ''
      this.queryParam.EndTime = ''
      this.tableRefresh(true)
    },
    tableRefresh(refresh = false) {
      this.$refs.table.refresh(refresh)
    },
    handleMultiple(type) {
      let content = ''
      if (type === 'all') {
        content = `确认通过当前全部${this.$refs.table.localDataSource.length}数据吗？`
      } else {
        if (this.$isEmpty(this.selectedRowKeys)) {
          this.$message.warning('至少选择一条房源数据')
          return
        }
        content = `确认通过所选的${this.selectedRowKeys.length}条房源吗？`
      }
      this.$confirm({
        title: '提示',
        content,
        onOk: () => {
          const data = type === 'all' ? this.$refs.table.localDataSource.map((e) => e.ID) : this.selectedRowKeys
          console.log('data', data)
          this.editDataHouse = data
          this.visibleHouse = true
          // return getConfirmHousesList({
          //   Mode: 'ConfirmHouses',
          //   UserName: this.$store.getters.nickname,
          //   HouseID: data.join(','),
          //   ProposerID: 0,
          //   Remark: values.Remark,
          //   UserID: this.$store.getters.userid,
          // }).then((res) => {
          //   this.$message.success('房源空置成功！')
          //   this.tableRefresh()
          // })
        },
      })
    },
    handleAction(record, type) {
      this.selectedRowKeys = []
      record.ProposerID = 0
      this.editData = record
      if (type === 'out') {
        this.visibleForm = true
      }
    },
  },
}
</script>
<style lang="less" scoped>
/deep/ .ant-table td {
  white-space: nowrap;
}
</style>
